package tenants

import (
	"context"
	"errors"
	"product/common/datetime"
	"strconv"

	"product/authorization/serve/internal/svc"
	"product/authorization/serve/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryTenantByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 查询租户信息
func NewQueryTenantByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryTenantByIdLogic {
	return &QueryTenantByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *QueryTenantByIdLogic) QueryTenantById(req *types.QueryTenantByIdReq) (resp *types.TenantsInfo, err error) {
	if req.ID <= 0 {
		return nil, errors.New("租户ID不能小于等于0")
	}
	tenant, err := l.svcCtx.TenantModel.FindTenantByID(l.ctx, req.ID)

	if err != nil {
		return nil, errors.New("没有租户信息")
	}
	return &types.TenantsInfo{
		ID:                strconv.FormatInt(tenant.ID, 10),
		Name:              tenant.Name,
		NickName:          tenant.NickName.String,
		Contact:           tenant.Contact,
		ContactPhone:      tenant.ContactPhone,
		Email:             tenant.Email.String,
		Telephone:         tenant.Telephone.String,
		Legal:             tenant.Legal,
		RegisteredAddress: tenant.RegisteredAddress,
		RegisteredCapital: tenant.RegisteredCapital.Float64,
		Code:              tenant.Code,
		CreatedDate:       datetime.NewUnix(tenant.CreatedDate).ToFormatByTp(datetime.YearMouthDayHourMinute),
		ModifiedDate:      datetime.NewUnix(tenant.ModifiedDate).ToFormatByTp(datetime.YearMouthDayHourMinute),
		RegistrationDate:  datetime.NewUnix(tenant.RegistrationDate).ToFormatByTp(datetime.YearMouthDayHourMinute),
		Validity:          int(tenant.Validity.Int64),
		Status:            int(tenant.Status),
		TaxID:             tenant.TaxID.String,
		TenantDomain:      tenant.TenantDomain,
		Type:              int(tenant.Type),
		DateOfActivation:  datetime.NewUnix(tenant.DateOfActivation.Int64).ToFormatByTp(datetime.YearMouthDayHourMinute),
		Region:            tenant.Region.String,
		EstablishmentDate: datetime.NewUnix(tenant.EstablishmentDate.Int64).ToFormatByTp(datetime.YearMouthDayHourMinute),
		ExpirationDate:    datetime.NewUnix(tenant.ExpirationDate).ToFormatByTp(datetime.YearMouthDayHourMinute),
	}, nil
}
